package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Roles;
import com.qfedu.mtl.service.RoleService;
import com.qfedu.mtl.service.impl.RoleServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;
import com.qfedu.mtl.vo.TreeVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @Date 2023/9/26 18:55
 * @Author DARKER
 * @Email 1402216045@qq.com
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    //声明业务组件(对象)
    RoleService roleService = new RoleServiceImpl();

    //处理添加角色请求的接口
    @RequestMapping("/add.do")
    public void addRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithText(request,response);
        Roles roles = HttpUtil.request2Bean(request, Roles.class);
        //调用业务逻辑添加
        int row = roleService.add(roles);
        if (row>0){
            response.getWriter().println("添加成功!");
        }else{
            response.getWriter().println("添加失败!");
        }

    }

    //处理删除角色的接口
    @RequestMapping("/del.do")
    public void delRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithText(request,response);
        String id = request.getParameter("id");
        //调用业务逻辑添加
        int row = roleService.delById(id);
        if (row>0){
            response.getWriter().println("删除成功!");
        }else{
            response.getWriter().println("删除失败!");
        }

    }

    //处理批量删除的接口
    @RequestMapping("/delBatch.do")
    public void delBatch(HttpServletRequest request, HttpServletResponse response) throws IOException{
        CharacterUtil.setContentWithText(request, response);
        String id = request.getParameter("ids");  //ids => 1,4,5
        String[] split = id.split(",");
        List<Integer> ids = Stream.of(split).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        try{
            roleService.delBatchById(ids);
            HttpUtil.response(null,0,"批量删除成功!",response);
        }catch (Exception e){
            HttpUtil.response(null,-1,"批量删除失败!\n"+e.getMessage(),response);
        }
    }

    //处理修改角色的接口
    @RequestMapping("/update.do")
    public void updateRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithText(request,response);
        Roles roles = HttpUtil.request2Bean(request, Roles.class);
        //调用业务逻辑添加
        int row = roleService.updateById(roles);
        if (row>0){
            response.getWriter().println("修改成功!");
        }else{
            response.getWriter().println("修改失败!");
        }
    }

    //处理查询角色的接口，查询渲染需要
    @RequestMapping("/allRoleList.do")
    public void RoleList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request,response);
        //调用业务逻辑获得所有角色信息
        List<Roles> roles= roleService.findAllRole();

        response.getWriter().println(JSON.toJSONString(roles));
    }


    //处理分页角色的接口
    @RequestMapping("/list.do")
    public void find(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request,response);
        String page = StrUtil.ifNull(request.getParameter("page"), "1");
        String size = StrUtil.ifNull(request.getParameter("size"), "5");
        Roles role = HttpUtil.request2Bean(request, Roles.class);
        //调用业务逻辑获得分页信息,附带分页条件
        Pager<Roles> roles= roleService.findPage(Integer.valueOf(page),Integer.valueOf(size),role);
        //使用专门的vo进行响应
        TableVO vo = TableVO.from(roles);
        //响应
        String json = JSON.toJSONString(vo);
        response.getWriter().print(json);

    }

    //处理角色授权的接口
    @RequestMapping("/menusTree.do")
    public void menusTree(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithJson(request, response);
        List<TreeVO> tree =  roleService.findAvailableMenus();
        //响应
        String json = JSON.toJSONString(tree);
        response.getWriter().println(json);
    }
    //处理角色授权的接口
    @RequestMapping("/grant.do")
    public void grant(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CharacterUtil.setContentWithText(request,response);
        Integer roleId =Integer.valueOf( request.getParameter("roleId") );
        String menuI = request.getParameter("menuId");
        String[] menuId = menuI.split(",");
        List<Integer> menuIds = Stream.of(menuId).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        //调用业务逻辑进行角色授权
        try {
            roleService.grant(roleId,menuIds);
            //响应
            response.getWriter().print("授权成功!");
        }catch (Exception e){
            response.getWriter().print("授权失败!  "+e.getMessage());

        }

    }


}
